<?php

namespace app\api\model;

use think\Model;
use think\Db;

class Claims extends Model
{
    private static $instance = null;

	public function __construct($data = [])
    {
        parent::__construct($data);
    }
    
    /**
     * 获得理赔列表
     * claims_sn 报案号； assured 出险人；  happen_time 出险时间
     * claim_status:0.已报案 1.已立案 2.待审核 3.审核成功 4.已结案 5.取消报案 6.拒绝
     * @param unknown $data
     */
    public function getClaims($data)
    {
        $where = [
            'uid' => $data['uid'],
        ];
        return Db::name('claim_order')->where($where)->field('id,claim_sn,assured,happen_time,claim_status')->order('id desc')->select();
    }
    
    /**
     * 获得案件详情
     */
    public function claimsDetail($data)
    {
        $claim = Db::name('claim_order c')
            ->join('employee_info_record e', 'c.num_id=e.num_id')
            ->where(array('c.id'=>$data['id']))
            ->field('c.id, c.claim_sn, c.assured, c.happen_time, c.claim_status, c.num_id, c.risk_desc, c.contact_phone,
                c.pro_name, e.s_time, e.e_time')
            ->find();
        
        $changlog = Db::name('claim_order_change_log')->field('claims_statement,payment_img')->where(array('claim_id'=>$data['id']))->select();
        foreach($changlog as $key => $val){
            if($val['claims_statement']){
                $claim['claims_statement'] = $val['claims_statement'];
            }
            if($val['payment_img']){
                $claim['payment_img'] = $val['payment_img'];
            }
        }
         
        $claim['claims_statement'] = isset($claim['claims_statement']) ? $claim['claims_statement'] : '';
        $claim['payment_img'] = isset($claim['payment_img']) ? $claim['payment_img'] : '';
        
        $claim['risk_desc'] = $this->handleRisk($claim['risk_desc']);
       
        $today = time();
        $s = strtotime($claim['s_time']);
        $e = strtotime($claim['e_time']);
        if(($s < $today) && ($e > $today)){
            $claim['type'] = 1;
        }else{
            $claim['type'] = 2;
        }
        
        return $claim;
    }
    
    /**
     * 添加理赔材料
     */
    public function addMaterial($data)
    {
        $update = [
            'medical_record' => $data['medical_record'],
            'hospital_invoice' => $data['hospital_invoice'],
            'cost_list' => $data['cost_list'],
            'photo_ID' => $data['photo_ID'],
            'payee_name' => $data['payee_name'],
            'payee_bank' => $data['payee_bank'],
            'bank_card' => $data['bank_card'],
            'update_time' => datetime(time(), 'Y-m-d H:i:s'),
            'claim_status' => 2
        ];
        $result = Db::name('claim_order')->where(array('id'=>$data['id']))->update($update);
        $code = $result ? 200 : 100;
        return array('code' => $code);
    }
    
    /**
     * 取消报案
     */
    public function cancelClaim($data)
    {
        $update = [
            'update_time' => datetime(time(), 'Y-m-d H:i:s'),
            'claim_status' => 5
        ];
        $result = Db::name('claim_order')->where(array('id'=>$data['id']))->update($update);
        $code = $result ? 200 : 100;
        return array('code' => $code);
    }
    
    
    /**
     * 处理保险原因
     * $claim['risk_desc']
     */
    public function handleRisk($data)
    {
        $reason = config('reason.list');
        $desc = explode(',', rtrim($data, ","));
        $risk = '';
        foreach($desc as $val){
            $risk .= $reason[$val].'、';
        }
        return mb_substr($risk, 0, -1);
    }
    
}

